<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>GenericAPIView\(二级\) | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../../03-day/view/view/mixin.html" />
    
    
    <link rel="prev" href="../../../03-day/view/view/apiview.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.2.2"
        data-chapter-title="GenericAPIView\(二级\)"
        data-filepath="03-day/view/view/genericapiview.md"
        data-basepath="../../.."
        data-revision="Sun May 10 2020 14:49:33 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Django REST framework
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01-day.html">
            
                
                    <a href="../../../01-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django REST framework流程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01-day/restful.html">
            
                
                    <a href="../../../01-day/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        RESTfull
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="01-day/restful/mode.html">
            
                
                    <a href="../../../01-day/restful/mode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="01-day/restful/restful.html">
            
                
                    <a href="../../../01-day/restful/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="01-day/restful/method.html">
            
                
                    <a href="../../../01-day/restful/method.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.</b>
                        
                        RESTful设计思想
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="01-day/restful/method/request.html">
            
                
                    <a href="../../../01-day/restful/method/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.1.</b>
                        
                        请求相关
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="01-day/restful/method/response.html">
            
                
                    <a href="../../../01-day/restful/method/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.2.</b>
                        
                        响应相关
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="01-day/restful/prepare.html">
            
                
                    <a href="../../../01-day/restful/prepare.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.4.</b>
                        
                        项目准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="01-day/restful/django.html">
            
                
                    <a href="../../../01-day/restful/django.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.5.</b>
                        
                        使用Django开发REST 接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="01-day/restful/task.html">
            
                
                    <a href="../../../01-day/restful/task.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.6.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01-day/restful/djangorestful-framework.html">
            
                
                    <a href="../../../01-day/restful/djangorestful-framework.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Django REST framework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01-day/restful/djangorestful-framework/config.html">
            
                
                    <a href="../../../01-day/restful/djangorestful-framework/config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="01-day/restful/djangorestful-framework/like.html">
            
                
                    <a href="../../../01-day/restful/djangorestful-framework/like.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.2.</b>
                        
                        Django REST framework魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02-day.html">
            
                
                    <a href="../../../02-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        序列化和反序列化
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="02-day/modify.html">
            
                
                    <a href="../../../02-day/modify.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Serializer字段和选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="02-day/serializer.html">
            
                
                    <a href="../../../02-day/serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="02-day/serializer/base.html">
            
                
                    <a href="../../../02-day/serializer/base.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.1.</b>
                        
                        基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="02-day/serializer/assoc.html">
            
                
                    <a href="../../../02-day/serializer/assoc.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.2.</b>
                        
                        关联对象嵌套序列化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="02-day/un-serializer.html">
            
                
                    <a href="../../../02-day/un-serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        反序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="02-day/un-serializer/validate.html">
            
                
                    <a href="../../../02-day/un-serializer/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.</b>
                        
                        验证
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1.1" data-path="02-day/un-serializer/validate/type.html">
            
                
                    <a href="../../../02-day/un-serializer/validate/type.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.1.</b>
                        
                        字段类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.2" data-path="02-day/un-serializer/validate/field.html">
            
                
                    <a href="../../../02-day/un-serializer/validate/field.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.2.</b>
                        
                        字段选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.3" data-path="02-day/un-serializer/validate/validatefieldname.html">
            
                
                    <a href="../../../02-day/un-serializer/validate/validatefieldname.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.3.</b>
                        
                        validate\_fieldname
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.4" data-path="02-day/un-serializer/validate/validate.html">
            
                
                    <a href="../../../02-day/un-serializer/validate/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.4.</b>
                        
                        validate
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="02-day/un-serializer/save.html">
            
                
                    <a href="../../../02-day/un-serializer/save.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.2.</b>
                        
                        保存
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="02-day/modelserializer.html">
            
                
                    <a href="../../../02-day/modelserializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03-day/view.html">
            
                
                    <a href="../../../03-day/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03-day/view/requestresponse.html">
            
                
                    <a href="../../../03-day/view/requestresponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Request 与 Response
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="03-day/view/requestresponse/request.html">
            
                
                    <a href="../../../03-day/view/requestresponse/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.1.</b>
                        
                        Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="03-day/view/requestresponse/response.html">
            
                
                    <a href="../../../03-day/view/requestresponse/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.2.</b>
                        
                        Response
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03-day/view/view.html">
            
                
                    <a href="../../../03-day/view/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="03-day/view/view/apiview.html">
            
                
                    <a href="../../../03-day/view/view/apiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.1.</b>
                        
                        APIView\(一级\)
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.2.2" data-path="03-day/view/view/genericapiview.html">
            
                
                    <a href="../../../03-day/view/view/genericapiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.2.</b>
                        
                        GenericAPIView\(二级\)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="03-day/view/view/mixin.html">
            
                
                    <a href="../../../03-day/view/view/mixin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.3.</b>
                        
                        Mixin
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="03-day/view/view/subclass.html">
            
                
                    <a href="../../../03-day/view/view/subclass.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.4.</b>
                        
                        子类视图\(三级\)
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03-day/view/viewset.html">
            
                
                    <a href="../../../03-day/view/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        视图集ViewSet
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="03-day/view/viewset/viewset.html">
            
                
                    <a href="../../../03-day/view/viewset/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="03-day/view/viewset/genericviewset.html">
            
                
                    <a href="../../../03-day/view/viewset/genericviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GenericViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="03-day/view/viewset/modelviewset.html">
            
                
                    <a href="../../../03-day/view/viewset/modelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        ModelViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="03-day/view/viewset/readonlymodelviewset.html">
            
                
                    <a href="../../../03-day/view/viewset/readonlymodelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        ReadOnlyModelViewSet
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="03-day/view/router.html">
            
                
                    <a href="../../../03-day/view/router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="03-day/other.html">
            
                
                    <a href="../../../03-day/other.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        高级功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="03-day/other/authentication.html">
            
                
                    <a href="../../../03-day/other/authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        认证Authentication
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="03-day/other/quan-xian-permissions.html">
            
                
                    <a href="../../../03-day/other/quan-xian-permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        权限Permissions
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="03-day/other/throttling.html">
            
                
                    <a href="../../../03-day/other/throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        限流Throttling
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="03-day/other/filtering.html">
            
                
                    <a href="../../../03-day/other/filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        过滤Filtering
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="03-day/other/order.html">
            
                
                    <a href="../../../03-day/other/order.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        排序OrderingFilter
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="03-day/other/pagination.html">
            
                
                    <a href="../../../03-day/other/pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        分页Pagination
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="03-day/other/exceptions.html">
            
                
                    <a href="../../../03-day/other/exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        异常处理 Exceptions
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="genericapiview">GenericAPIView</h1>
<p><a href="http://www.django-rest-framework.org/api-guide/generic-views/#genericapiview" target="_blank">&#x6587;&#x6863;</a></p>
<p><code>rest_framework.generics.GenericAPIView</code></p>
<p><img src="../../../assets/GenericAPIView.png" alt=""></p>
<p>&#x7EE7;&#x627F;&#x81EA;<code>APIVIew</code>&#xFF0C;&#x589E;&#x52A0;&#x4E86;&#x5BF9;&#x4E8E;&#x5217;&#x8868;&#x89C6;&#x56FE;&#x548C;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x53EF;&#x80FD;&#x7528;&#x5230;&#x7684;&#x901A;&#x7528;&#x652F;&#x6301;&#x65B9;&#x6CD5;&#x3002;&#x901A;&#x5E38;&#x4F7F;&#x7528;&#x65F6;&#xFF0C;&#x53EF;&#x642D;&#x914D;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;Mixin&#x6269;&#x5C55;&#x7C7B;&#x3002;</p>
<p><strong>&#x4F7F;&#x7528;GenericAPIView&#x7C7B;&#x4E00;&#x822C;&#x9700;&#x8981;&#x5B9E;&#x73B0;queryset&#x5C5E;&#x6027;&#x6216;&#x8005;&#x91CD;&#x5199;get_queryset&#x65B9;&#x6CD5;</strong></p>
<p><img src="../../../assets/GenericAPIVew&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;.png" alt=""></p>
<h2 id="&#x652F;&#x6301;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#xFF1A;">&#x652F;&#x6301;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#xFF1A;</h2>
<ul>
<li><h3 id="&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x901A;&#x7528;&#xFF1A;">&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x901A;&#x7528;&#xFF1A;</h3>
<ul>
<li><strong>queryset</strong>&#x5217;&#x8868;&#x89C6;&#x56FE;&#x7684;&#x67E5;&#x8BE2;&#x96C6;</li>
<li><strong>serializer_class</strong>&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5668;</li>
</ul>
</li>
<li><h3 id="&#x8BE6;&#x60C5;&#x9875;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#xFF1A;">&#x8BE6;&#x60C5;&#x9875;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#xFF1A;</h3>
<ul>
<li><strong>lookup_field</strong>&#x67E5;&#x8BE2;&#x5355;&#x4E00;&#x6570;&#x636E;&#x5E93;&#x5BF9;&#x8C61;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x6761;&#x4EF6;&#x5B57;&#x6BB5;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&apos;<code>pk</code>&apos;</li>
<li><strong>lookup_url_kwarg</strong>&#x67E5;&#x8BE2;&#x5355;&#x4E00;&#x6570;&#x636E;&#x65F6;URL&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x5173;&#x952E;&#x5B57;&#x540D;&#x79F0;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E0E;<strong>look_field</strong>&#x76F8;&#x540C;</li>
</ul>
</li>
</ul>
<h2 id="&#x63D0;&#x4F9B;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;">&#x63D0;&#x4F9B;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</h2>
<ul>
<li><h3 id="&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x901A;&#x7528;&#xFF1A;">&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x901A;&#x7528;&#xFF1A;</h3>
<ul>
<li><p><strong>get_queryset(self)</strong></p>
<p>&#x8FD4;&#x56DE;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#x7684;&#x67E5;&#x8BE2;&#x96C6;&#xFF0C;&#x662F;&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x7684;&#x57FA;&#x7840;&#xFF0C;&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;<code>queryset</code>&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-py"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_queryset</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-keyword">return</span> BookInfo.objects.all()
</code></pre>
</li>
<li><p><strong>get_serializer_class(self)</strong></p>
<p>&#x8FD4;&#x56DE;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7C7B;&#xFF0C;&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;<code>serializer_class</code>&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-py"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_serializer_class</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-keyword">return</span> BookInfoModelSerializer
</code></pre>
</li>
<li><h5 id="getserializerself-args-kwargs">get_serializer(self,_args, *_kwargs) </h5>
<p>&#x8FD4;&#x56DE;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x88AB;&#x5176;&#x4ED6;&#x89C6;&#x56FE;&#x6216;&#x6269;&#x5C55;&#x7C7B;&#x4F7F;&#x7528;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5728;&#x89C6;&#x56FE;&#x4E2D;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#x3002;</p>
<blockquote>
<p><strong>&#x6CE8;&#x610F;&#xFF0C;&#x5728;&#x63D0;&#x4F9B;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#xFF0C;REST framework&#x4F1A;&#x5411;&#x5BF9;&#x8C61;&#x7684;context&#x5C5E;&#x6027;&#x8865;&#x5145;&#x4E09;&#x4E2A;&#x6570;&#x636E;&#xFF1A;request&#x3001;format&#x3001;view&#xFF0C;&#x8FD9;&#x4E09;&#x4E2A;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5728;&#x5B9A;&#x4E49;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x65F6;&#x4F7F;&#x7528;&#x3002;</strong></p>
</blockquote>
</li>
</ul>
</li>
<li><h3 id="&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#xFF1A;">&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#xFF1A;</h3>
<ul>
<li><p><strong>get_object(self)</strong>&#x8FD4;&#x56DE;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x6240;&#x9700;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;<code>lookup_field</code>&#x53C2;&#x6570;&#x6765;&#x8FC7;&#x6EE4;queryset&#x3002; &#x5728;&#x8BD5;&#x56FE;&#x4E2D;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x8BE6;&#x60C5;&#x4FE1;&#x606F;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x82E5;&#x8BE6;&#x60C5;&#x8BBF;&#x95EE;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x4F1A;&#x8FD4;&#x56DE;404&#x3002;</p>
</li>
</ul>
</li>
</ul>
<h2 id="&#x5217;&#x8868;&#x89C6;&#x56FE;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;&#x4E3E;&#x4F8B;">&#x5217;&#x8868;&#x89C6;&#x56FE;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;&#x4E3E;&#x4F8B;</h2>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> rest_framework.generics <span class="hljs-keyword">import</span> GenericAPIView
<span class="hljs-keyword">from</span> book.models <span class="hljs-keyword">import</span> BookInfo
<span class="hljs-keyword">from</span> book.serializers <span class="hljs-keyword">import</span> BookInfoModelSerializer

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookListGenericView</span><span class="hljs-params">(GenericAPIView)</span>:</span>

    <span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x5C5E;&#x6027;</span>
    queryset = BookInfo.objects.all()

    <span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5C5E;&#x6027;</span>
    serializer_class = BookInfoModelSerializer

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self,request)</span>:</span>
        <span class="hljs-comment"># 1.&#x67E5;&#x8BE2;&#x6570;&#x636E;</span>
        books = self.get_queryset()
        <span class="hljs-comment"># 2.&#x521B;&#x5EFA;&#x5E8F;&#x5217;&#x5316;&#x5668;&#xFF0C;&#x5E76;&#x4F20;&#x9012;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#xFF08;&#x8BBE;&#x7F6E;many=True&#xFF09;</span>
        serializer = self.get_serializer(books, many=<span class="hljs-keyword">True</span>)
        <span class="hljs-comment"># 3.&#x8FD4;&#x56DE;&#x54CD;&#x5E94; serializer.data &#x5C31;&#x662F;&#x5B57;&#x5178;&#x5217;&#x8868;</span>
        <span class="hljs-keyword">return</span> Response(serializer.data)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self,request)</span>:</span>
        <span class="hljs-comment"># 1.&#x63A5;&#x6536;&#x53C2;&#x6570;</span>
        book_dict = request.data
        <span class="hljs-comment"># 2.&#x521B;&#x5EFA;&#x5E8F;&#x5217;&#x5316;&#x5668;&#xFF0C;&#x5E76;&#x4F20;&#x9012;&#x53C2;&#x6570;</span>
        serializer = self.get_serializer(data=book_dict)
        <span class="hljs-comment"># 3.&#x9A8C;&#x8BC1;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x6839;&#x636E;&#x9A8C;&#x8BC1;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x5224;&#x65AD;</span>
        <span class="hljs-keyword">if</span> serializer.is_valid():
            <span class="hljs-comment"># 4.&#x6570;&#x636E;&#x9A8C;&#x8BC1;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x8C03;&#x7528;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7684;save&#x65B9;&#x6CD5;&#x3002;&#x6765;&#x89E6;&#x53D1;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7684;create&#x65B9;&#x6CD5;</span>
            <span class="hljs-comment"># create&#x65B9;&#x6CD5;&#x4F1A;&#x8C03;&#x7528;&#x6A21;&#x578B;&#x6765;&#x4FDD;&#x5B58;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;</span>
            serializer.save()
            <span class="hljs-comment"># 5. &#x8FD4;&#x56DE;&#x54CD;&#x5E94; serializer.data &#x5C31;&#x662F;&#x5B57;&#x5178;&#x6570;&#x636E;</span>
            <span class="hljs-keyword">return</span> Response(serializer.data)
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># &#x8FD4;&#x56DE;&#x9519;&#x8BEF;&#x4FE1;&#x606F;</span>
            <span class="hljs-keyword">return</span> Response({<span class="hljs-string">&apos;msg&apos;</span>: <span class="hljs-string">&apos;&#x4FDD;&#x5B58;&#x5931;&#x8D25;&apos;</span>})
</code></pre>
<p>&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x65B9;&#x6CD5;&#x6765;&#x5B9E;&#x73B0;</p>
<pre><code>class BookListGenericView(GenericAPIView):

    # &#x8BBE;&#x7F6E;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x5C5E;&#x6027;
    # queryset = BookInfo.objects.all()
    #&#x91CD;&#x5199;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x65B9;&#x6CD5;
    def get_queryset(self):
        return BookInfo.objects.all()

    # &#x8BBE;&#x7F6E;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5C5E;&#x6027;
    # serializer_class = BookInfoModelSerializer
    # &#x91CD;&#x5199;&#x8FD4;&#x56DE;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x65B9;&#x6CD5;
    def get_serializer_class(self):
        return BookInfoModelSerializer
</code></pre><p><img src="../../../assets/GenericAPIView&#x4E0E;APIView&#x7684;&#x5BF9;&#x6BD4;.png" alt=""></p>
<h2 id="&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;&#x4E3E;&#x4F8B;">&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;&#x4E3E;&#x4F8B;</h2>
<pre><code>from rest_framework.generics import GenericAPIView
from book.models import BookInfo
from book.serializers import BookInfoModelSerializer

class BookDetailGenericView(GenericAPIView):
    # &#x8BBE;&#x7F6E;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x5C5E;&#x6027;
    queryset = BookInfo.objects.all()
    # &#x91CD;&#x5199;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x65B9;&#x6CD5;
    # def get_queryset(self):
    #     return BookInfo.objects.all()

    # &#x8BBE;&#x7F6E;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5C5E;&#x6027;
    serializer_class = BookInfoModelSerializer
    # &#x91CD;&#x5199;&#x8FD4;&#x56DE;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x65B9;&#x6CD5;
    # def get_serializer_class(self):
    #     return BookInfoModelSerializer

    # &#x9ED8;&#x8BA4;&#x662F;pk
    # &#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x4E3A; id, &#x5982;&#x679C;&#x4FEE;&#x6539;&#xFF0C;
    # &#x5219; get,put,delete &#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x90FD;&#x8981;&#x6539;&#x53D8;
    lookup_field = &apos;pk&apos;

    def get(self,request,pk):
        #1.&#x67E5;&#x8BE2;&#x6307;&#x5B9A;&#x4E66;&#x7C4D;
        book = self.get_object()
        #2.&#x521B;&#x5EFA;&#x5E8F;&#x5217;&#x5316;&#x5668;&#xFF0C;&#x4F20;&#x9012;&#x6A21;&#x578B;&#x5BF9;&#x8C61;
        serializer = self.get_serializer(book)
        #3.&#x83B7;&#x53D6;&#x5B57;&#x5178;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x54CD;&#x5E94;
        return Response(serializer.data)

    def put(self,request,pk):
        #1.&#x67E5;&#x8BE2;&#x6307;&#x5B9A;&#x4E66;&#x7C4D;
        book = self.get_object()
        #2.&#x521B;&#x5EFA;&#x5E8F;&#x5217;&#x5316;&#x5668;&#xFF0C;&#x4F20;&#x9012;&#x6A21;&#x578B;&#x5BF9;&#x8C61;&#x548C;&#x63A5;&#x6536;&#x7684;&#x53C2;&#x6570;
        serializer = self.get_serializer(instance=book,data=request.data)
        # 3.&#x9A8C;&#x8BC1;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x6839;&#x636E;&#x9A8C;&#x8BC1;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x5224;&#x65AD;
        if serializer.is_valid():
            # 4.&#x6570;&#x636E;&#x9A8C;&#x8BC1;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x8C03;&#x7528;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7684;save&#x65B9;&#x6CD5;&#x3002;&#x6765;&#x89E6;&#x53D1;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7684;update&#x65B9;&#x6CD5;
            # update&#x65B9;&#x6CD5;&#x4F1A;&#x8C03;&#x7528;&#x6A21;&#x578B;&#x6765;&#x66F4;&#x65B0;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;
            serializer.save()
            # 5. &#x8FD4;&#x56DE;&#x54CD;&#x5E94; serializer.data &#x5C31;&#x662F;&#x5B57;&#x5178;&#x6570;&#x636E;
            return Response(serializer.data)
        else:
            # &#x8FD4;&#x56DE;&#x9519;&#x8BEF;&#x4FE1;&#x606F;
            return Response({&apos;msg&apos;: &apos;&#x4FDD;&#x5B58;&#x5931;&#x8D25;&apos;})

    def delete(self,request,pk):
        #1.&#x67E5;&#x8BE2;&#x6307;&#x5B9A;&#x4E66;&#x7C4D;
        book = self.get_object()
        #2.&#x5220;&#x9664;
        book.delete()
        #3.&#x8FD4;&#x56DE;&#x54CD;&#x5E94;
        from rest_framework import status
        return Response(status=status.HTTP_204_NO_CONTENT)
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../../03-day/view/view/apiview.html" class="navigation navigation-prev " aria-label="Previous page: APIView\(一级\)"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../../03-day/view/view/mixin.html" class="navigation navigation-next " aria-label="Next page: Mixin"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../../gitbook/app.js"></script>

    
    <script src="../../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../../gitbook/plugins/gitbook-plugin-fontsettings/fontsettings.js"></script>
    

    
    <script src="../../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
